<?php
fps_define('FPS_VIEW_ROOT', APP_ROOT . '/views');
fps_define('FPS_VIEW_DEFAULT_LAYOUT', 'default');

class fpsView {
    static $vars = array();

    static function set($name, $value = null) {
        if (is_array($name)) {
            self::$vars = array_merge(self::$vars, $name);
        }
        elseif (!empty($name)) {
            self::$vars[$name] = $value;
        }
    }

    static function render($view, $data = null, $layout = FPS_VIEW_DEFAULT_LAYOUT) {
        extract(self::$vars);

        if (is_array($data)) {
            extract($data);
        }

        include(FPS_VIEW_ROOT . "/page/$view.php");

        if ($layout) {
            $FPS_PAGE_CONTENT = ob_get_clean();
            include(FPS_VIEW_ROOT . "/layout/$layout.php");
        }
    }
    private static function layout($layout = 'default', $data = null) {
        if (is_array($data)) {
            extract($data);
        }
        $FPS_PAGE_CONTENT = ob_get_clean();
        include(FPS_VIEW_ROOT . "/layout/$layout.php");
    }

    static function message($title, $message, $buttons=array('Back' => -1)) {
        echo "<h3>$title</h3><p>{$message}</p>";
        foreach ($buttons as $name => $uri) {
            $name = htmlspecialchars($name);
            if (is_int($uri)) {
                echo "<button onclick=\"history.go($uri)\">$name</button>";
            }
            else {
                $uri = htmlspecialchars($uri);
                echo "<button href=\"$uri\" onclick=\"location.href=this.href;\">$name</button>";
            }
        }
        self::layout('message');
        exit;
    }

}
